<!-- 主应用组件 - 开发者：余亚涛（整合） -->
<template>
  <div>
    <AuthComponent 
      v-if="!isLoggedIn" 
      @login-success="handleLoginSuccess"
    />
    <div v-else>
      <TodoHeader @add-todo="handleAddTodo" />
      <div class="container">
        <TodoList 
          :todos="todoStore.data.todo"
          @remove="handleRemove"
          @toggle="handleToggle"
        />
        <CompletedList 
          :completed="todoStore.data.completed"
          @remove="handleRemove"
          @toggle="handleToggle"
        />
      </div>
      <!-- 欢迎弹窗 -->
      <div v-if="showWelcomePopup" class="welcome-popup" @click="closeWelcomePopup">
        <div class="welcome-popup-content" @click.stop>
          <h3>欢迎，{{ currentUser }}！</h3>
          <p>开始管理您的待办事项吧！</p>
          <button @click="closeWelcomePopup" class="close-btn">确定</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useTodoStore } from './TodoStore.js';
import AuthComponent from './AuthComponent.vue';
import TodoHeader from './TodoHeader.vue';
import TodoList from './TodoList.vue';
import CompletedList from './CompletedList.vue';

const isLoggedIn = ref(false);
const currentUser = ref('');
const showWelcomePopup = ref(false);
const todoStore = useTodoStore();

const handleLoginSuccess = (username) => {
  isLoggedIn.value = true;
  currentUser.value = username;
  todoStore.loadData();
  // 显示欢迎弹窗
  showWelcomePopup.value = true;
};

const handleAddTodo = (text) => {
  todoStore.addTodo(text);
};

const handleRemove = (index, isCompleted) => {
  todoStore.removeTodo(index, isCompleted);
};

const handleToggle = (index, isCompleted) => {
  todoStore.toggleComplete(index, isCompleted);
};

const closeWelcomePopup = () => {
  showWelcomePopup.value = false;
};
</script>

<style scoped>
/* 欢迎弹窗样式 */
.welcome-popup {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.welcome-popup-content {
  background: white;
  padding: 30px;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  text-align: center;
  max-width: 400px;
  width: 90%;
  animation: popupFadeIn 0.3s ease-out;
}

@keyframes popupFadeIn {
  from {
    opacity: 0;
    transform: scale(0.8) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.welcome-popup-content h3 {
  margin: 0 0 15px 0;
  font-size: 24px;
  color: #25b99a;
  font-weight: 500;
}

.welcome-popup-content p {
  margin: 0 0 25px 0;
  font-size: 16px;
  color: #666;
  line-height: 1.5;
}

.close-btn {
  background: #25b99a;
  color: white;
  border: none;
  padding: 12px 30px;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: #1e9b7d;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(37, 185, 154, 0.3);
}
</style>
